package org.carabiner.infinitest.swingui;

import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;

/**
 * 
 * @author <a href="mailto:benrady@gmail.com"Ben Rady</a>
 */
class TreeModelExpansionListener implements TreeModelListener
{
  private JTree tree;

  public TreeModelExpansionListener(JTree testTree)
  {
    tree = testTree;
  }

  public void treeNodesChanged(TreeModelEvent e)
  {
    processEvent(e);
  }

  public void treeNodesInserted(TreeModelEvent e)
  {
    processEvent(e);
  }

  public void treeNodesRemoved(TreeModelEvent e)
  {
    processEvent(e);
  }

  public void treeStructureChanged(TreeModelEvent e)
  {
    processEvent(e);
  }

  private void processEvent(TreeModelEvent e)
  {
    // if (!SwingUtilities.isEventDispatchThread())
    // throw new IllegalStateException("Tree modifications must be done on the event queue");
    tree.expandPath(e.getTreePath());
  }
}
